/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.test.beans;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jbfilter.test.beans.util.ToStringUtils;

/**
 * A composer's work.
 * @author Marcus Adrian
 */
public class Work implements Cloneable {

	private Long id;
	private String title;
	private Category category;
	private Composer composer;
	private Date dateOfCreation;
	
	@Override
	public Work clone() {
		Work clone = new Work();
		clone.setId(id);
		clone.setTitle(title);
		clone.setComposer(composer == null ? null : composer.clone());
		clone.setDateOfCreation(new Date(dateOfCreation.getTime()));
		clone.setCategory(category);
		return clone;
	}
	
	@Override
	public String toString() {
		ToStringBuilder builder = ToStringUtils.getToStringBuilderOneLine(this);
		builder.append("id", id);
	    builder.append("title", title);
	    builder.append("category", category);
		if (composer == null) {
			builder.append("composer", "null");
		} else {
			builder.append("composer.name", composer.getName());
		}
		builder.append("dateOfCreation", dateOfCreation);
		return builder.toString();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public Composer getComposer() {
		return composer;
	}
	public void setComposer(Composer composer) {
		this.composer = composer;
	}
	public Date getDateOfCreation() {
		return dateOfCreation;
	}
	public void setDateOfCreation(Date dateOfCreation) {
		this.dateOfCreation = dateOfCreation;
	}
	/**
	 * The year of the work's creation or {@code -1} if the {@code dateOfCreation} is {@code null}.
	 * @return
	 */
	public int getYearOfCreation() {
		if (dateOfCreation == null) {
			return -1;
		}
		return Integer.valueOf(DateFormatUtils.format(dateOfCreation, "yyyy")).intValue();
	}
	
	/**
	 * Indicates if the work is an opera or ballet.
	 * @return
	 */
	public boolean isScenic() {
		return category == Category.OPERA || category == Category.BALLET;
	}
}
